#include "Ground.h"
#include <string>
#include <iostream>
#include <sstream>
#include <time.h>
#include "stdafx.h"


const float K_GROUND_HEIGHT = 50.f;
const float K_GROUND_WIDTH = 25000.f;
const float K_MAX_HEIGHT = 300.f;

const int K_NUM_BOXES = 100;

const sf::Color groundColour(60, 60, 60, 255); 

Ground::Ground(b2World* wo, WindowManager* win)
{
	world = wo;
	window = win;

	generateGround();
	generateGroundBoxes();

	float x = K_GROUND_WIDTH/2;
	float y = window->getHeight() - K_GROUND_HEIGHT/2;

	
	
	b2BodyDef bodyDef;
	bodyDef.position = b2Vec2(x/window->getScale(), y/window->getScale());
    bodyDef.type = b2_staticBody;
    body = world->CreateBody(&bodyDef);

	b2PolygonShape Shape;
	//b2Vec2 vertices[4];
	//int j = 0;
	//for(int i = 0; i < 4; i++)
	//{
	//	vertices[i] = getB2(points[i][0], points[i][1]);
	//	j++;
	//}
	
	//vertices[3] = b2Vec2((K_GROUND_WIDTH/2)/window->getScale(), (K_GROUND_HEIGHT/2)/window->getScale());
	//vertices[2] = b2Vec2((K_GROUND_WIDTH/2)/window->getScale(), -(K_GROUND_HEIGHT/2)/window->getScale());
	//vertices[1] = b2Vec2(0/window->getScale(), -100/window->getScale());
	//vertices[0] = b2Vec2(-(K_GROUND_WIDTH/2)/window->getScale(), -(K_GROUND_HEIGHT/2)/window->getScale());
	//Shape.Set(vertices, 4);
	Shape.SetAsBox((K_GROUND_WIDTH/2)/window->getScale(), (K_GROUND_HEIGHT/2)/window->getScale()); // Creates a box shape. Divide your desired width and height by 2.
    b2FixtureDef FixtureDef;
    FixtureDef.density = 0.f;  // Sets the density of the body
    FixtureDef.shape = &Shape; // Sets the shape
    body->CreateFixture(&FixtureDef); // Apply the fixture definition



	//mapBoxCount = 4;

	//boxes = new MapBox[4];

	//boxes[0].init(window, world, 600, 40, 4500, 20, getTop());
	//boxes[1].init(window, world, 400, 40, 4500, 40+20, getTop());
	//boxes[2].init(window, world, 200, 40, 4500, 40+40+20, getTop());
	//boxes[3].init(window, world, 100, 40, 4500, 40+40+40+20, getTop());

	//boxes[1]

}


Ground::~Ground(void)
{
}

void Ground::drawGround()
{

	//std::stringstream ss;

	//ss << (body->GetPosition().y * window->getScale())+K_GROUND_HEIGHT/2 << "\n";
	//OutputDebugString(ss.str().c_str());
	//

	sf::RectangleShape polygon(sf::Vector2f(K_GROUND_WIDTH, K_GROUND_HEIGHT));
	polygon.setFillColor(groundColour);
    polygon.setOrigin(K_GROUND_WIDTH/2, (K_GROUND_HEIGHT/2));
	polygon.setPosition((body->GetPosition().x * window->getScale()), (body->GetPosition().y * window->getScale()));
    polygon.setRotation(180/b2_pi * body->GetAngle());
	
    window->draw(polygon);


	//for (int i = 0; i < mapBoxCount; i++)
	//{
	//	boxes[i].drawBox(window, getTop(), groundColour);
	//}

	for (int i = 0; i < groundBoxes.size(); i++)
	{
		groundBoxes[i].drawBox(window, getTop(), groundColour);
	}

}

float Ground::getTop()
{
	return window->getHeight()-K_GROUND_HEIGHT;
}


float Ground::getHeight()
{
	return K_GROUND_HEIGHT;
}

float Ground::getWidth()
{
	return K_GROUND_WIDTH;
}

b2Vec2 Ground::getB2(float x, float y)
{
	b2Vec2 b2(x/window->getScale(), (window->getHeight()-y)/window->getScale());
	return b2;
	//vertices[4] = b2Vec2(-(K_GROUND_WIDTH/2)/window->getScale(), (K_GROUND_HEIGHT/2)/window->getScale());
}



void Ground::generateGround()
{
	int current = 0;
	srand(time(NULL));
	int maxLevel = ((K_MAX_HEIGHT-K_GROUND_HEIGHT)/50.f);
	for (int i = 0; i < K_NUM_BOXES; i++)
	{
		int random = (rand() % 3) - 1;
		if (current == 0 && random < 0)
		{
			random = 0;
		}
		else if (current == maxLevel && random > 0)
		{
			random = 0;
		}
		current += random;
		groundLayout.push_back(current);
	}
}

void Ground::generateGroundBoxes()
{
	for (int i = 0; i < groundLayout.size(); i++)
	{
		MapBox box;
		float w = K_GROUND_WIDTH/K_NUM_BOXES;
		float h = ((float)groundLayout[i])*50.f;
		float xLoc = i*(K_GROUND_WIDTH/K_NUM_BOXES);
		float yLoc = (((float)groundLayout[i])*50.f)/2.f;
		float groundHeight = getTop();
		box.init(window, world, w, h, xLoc, yLoc, groundHeight);
		groundBoxes.push_back(box);
	}
}